package com.example.lib.c1;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import static java.lang.Thread.sleep;

/**
 * 有两种方法可以创建一个新的执行线程
 */
public class Main2StartThread {

    private static class Thread1 extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread1 run...");
        }
    }

    private static class Thread2 implements Runnable {

        @Override
        public void run() {
            System.out.println("thread2 run...");
        }
    }

    //实现callable 这种有返回值 也归属于Runnable方式
    static class Thread3 implements Callable<String> {

        @Override
        public String call() throws Exception {
            System.out.println("thread3 run...");
            sleep(5000);
            return "thread3 return";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //第一种方法继承Thread
        new Thread1().start();

        //第二种方式实现runable接口
        new Thread(new Thread2()).start();

        //有返回值
        Thread3 thread3 = new Thread3();
        FutureTask<String> futureTask = new FutureTask<>(thread3);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());//阻塞
        System.out.println("1234");
    }
}
